package com.ptszyxx.popose.module.main.game;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.constants.BundleConstant;
import com.ptszyxx.popose.databinding.FragmentGameCommentBinding;
import com.ptszyxx.popose.module.main.game.adapter.GameCommentAdapter;
import com.ptszyxx.popose.module.main.game.vm.GameCommentVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.game.GameCommentEntity;
import com.ysg.http.data.entity.game.GameEntity;
import com.ysg.utils.YRecyclerViewUtil;
import com.ysg.utils.YStringUtil;

import java.util.List;

public class GameCommentFragment extends BaseFragment<FragmentGameCommentBinding, GameCommentVM> {
    private GameCommentAdapter adapter;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_game_comment;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public GameCommentVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(GameCommentVM.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        Bundle bundle = getArguments();
        if (bundle != null){
            viewModel.entity.set((GameEntity) bundle.getParcelable(BundleConstant.ENTITY));
        }
    }

    @Override
    public void initView() {
        super.initView();
        try {
            if (YStringUtil.isNotEmpty(viewModel.entity.get().getScore())){
                binding.ratingBar1.setRating(Float.valueOf(viewModel.entity.get().getScore()));
                binding.ratingBar2.setRating(Float.valueOf(viewModel.entity.get().getScore()));
                binding.ratingBar3.setRating(Float.valueOf(viewModel.entity.get().getScore()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        adapter = new GameCommentAdapter();
        YRecyclerViewUtil.init(getContext(), binding.recyclerView, adapter);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestList(true);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshEvent.observe(this, new Observer<List<GameCommentEntity>>() {
            @Override
            public void onChanged(List<GameCommentEntity> list) {
                setAdapterUI(list);
            }
        });
    }

    private void setAdapterUI(List<GameCommentEntity> list) {
        adapter.setList(list);
    }
}
